/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2006
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.full.dialogs;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import br.ufmg.dees.insane.commons.dialogs.TabbedDialog;
import br.ufmg.dees.insane.model.disc.element.ParametricElement;
import br.ufmg.dees.insane.model.disc.element.force.ElementSide;
import br.ufmg.dees.insane.model.disc.element.force.Side;
import br.ufmg.dees.insane.model.disc.node.Node;
import br.ufmg.dees.insane.ui.rich.full.InsaneInternalInterface;

/**
 * A class representing creation dialog on screen.
 * This dialog is provided by some tabs like limits,
 * grid and General settings.
 * For example:
 * <pre>
 *    CreateSettingsDialog dgl = new CreateSettingsDialog(parent, this);
 *    dgl.pack();
 * </pre>
 *
 * @author Pitangueira, Roque & Brugiolo, Marco 
 * @version 0.0.1 Beta  
 * @see gui.InsaneInternalInterface
 * @since jdk1.4.1
 */
public final class SurfaceForcesDialog extends TabbedDialog {

	private static final long serialVersionUID = 1L;
	private InsaneInternalInterface parent;
	private JPanel materialPane = new JPanel();
	private JPanel mPane = new JPanel();
	private JPanel mPane1 = new JPanel();
	private JPanel mPane2 = new JPanel();
	private JPanel mPane21 = new JPanel();
	private JPanel mPane22 = new JPanel();
	private JTable table;
	private String[] columnNames = new String[7];
	private String[][] cells;
	private ElementSide elmSide;
	protected ArrayList sides = new ArrayList();
	
	/**
	 * Constructor method, calling super with the especific title.
	 *
	 * @param parent The parent of the class.
	 * @see gui.InsaneInternalInterface
	 */
	public SurfaceForcesDialog(InsaneInternalInterface parent, ParametricElement element) {
		
		this.parent = parent;
		elmSide = new ElementSide(element);
		elmSide.mountSide();
		sides = elmSide.getSidesList();
		
		columnNames[0] = parent.getResourceBundle().getString("dSurfForcDialog01");
		columnNames[1] = "Fx";
		columnNames[2] = "Fy";
		columnNames[3] = "Fz";
		columnNames[4] = "Mx";
		columnNames[5] = "My";
		columnNames[6] = "Mz";
		
		int aux = elmSide.getNumberOfSides();
		
		if ( element.getType(1)=='L' ) {
			if ( element.getType(0)=='T' ) {
				aux = (int)(((Math.sqrt(1+8*aux)-1)/2)-1)*3;
			} else {
				aux = (int)(Math.sqrt(aux)-1)*4;
			}
		}
		cells = new String[aux][7];
		for(int i = 0; i < aux; i++) {
			cells[i][0] = ((Node)((Side)(sides.get(i))).getNodesList().get(0)).getLabel() + " - " + 
							((Node)((Side)(sides.get(i))).getNodesList().get(1)).getLabel();
		}
		
		materialPane.setLayout(new BoxLayout(materialPane, BoxLayout.Y_AXIS));
		mPane.setLayout(new BoxLayout(mPane, BoxLayout.X_AXIS));
		materialPane.add(mPane);

		mPane1.setLayout(new GridLayout(2,1,0,0));
		if ( aux<26 )
			mPane1.setMaximumSize(new Dimension(76,50));
		else
			mPane1.setMaximumSize(new Dimension(64,50));
		mPane.add(mPane1);
		mPane2.setLayout(new GridLayout(2,2,0,0));
		if ( aux<26 )
			mPane2.setMaximumSize(new Dimension(468,50));
		else
			mPane2.setMaximumSize(new Dimension(387,50));
		mPane.add(mPane2);
		if ( aux>=25 )
			mPane.add( new JLabel("    ") );
		mPane21.setLayout(new GridLayout(1,3,0,0));
		mPane22.setLayout(new GridLayout(1,3,0,0));

		mPane1.add( new JButton("") );
		mPane1.add( new JButton(parent.getResourceBundle().getString("dSurfForcDialog01")) );
		mPane2.add( new JButton(parent.getResourceBundle().getString("dSurfForcDialog02")) );
		mPane2.add( new JButton(parent.getResourceBundle().getString("dSurfForcDialog03")) );
		mPane21.add( new JButton("Fx") );
		mPane21.add( new JButton("Fy") );
		mPane21.add( new JButton("Mz") );
		mPane22.add( new JButton("Fx") );
		mPane22.add( new JButton("Fy") );
		mPane22.add( new JButton("Mz") );
		mPane2.add(mPane21);
		mPane2.add(mPane22);

		JPanel materialPropPane = new JPanel();
		materialPane.add(materialPropPane);
		materialPropPane.setLayout(new GridLayout(1,1,0,0));

		table = new JTable(cells, columnNames);
		if ( aux<26 ) {
			materialPropPane.add( table );
		} else {
			materialPropPane.add(new JScrollPane(table), "Center");
		}
		String label = parent.getResourceBundle().getString("dSurfForcDialog04") + " " + element.getLabel();
		addTab(label, materialPane);

	} // End of constructor

	public int showDialog() {
		return super.showDialog(parent, parent.getResourceBundle().getString("dSurfForcDialog05"));
	}

	/**
	 * This method is executed on OK button click.
	 */
	public void approvedPerform() {
		try {
			this.setVisible(false);
		} catch (Exception e) {
			Toolkit.getDefaultToolkit().beep();
			this.setVisible(false);
		}
	}

	/** Returns the Surface Force of the element.
	*@return The Surface Force of the element.
	*/
	public String[][] getSurfaceForce() {
		return cells;
	}

}